package com.ly.system.auth;

import com.ly.config.response.Result;

import com.ly.system.entity.TbUser;
import com.ly.system.mapper.TbUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Service;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

@Service("myAuthenticationSuccessHandlerimplements")
class MyAuthenticationSuccessHandlerimplements extends JsonAuthentication implements AuthenticationSuccessHandler {//登录成功处理逻辑
    @Autowired
    TbUserMapper sysUserMapper;

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException
            , ServletException {
        //更新用户表上次登录时间、更新人、更新时间等字段
//        User userDetails = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//        TbUser sysUser = sysUserMapper.selectByName(userDetails.getUsername());
//        sysUser.setLastLoginTime(new Date());
//        sysUser.setUpdateTime(new Date());
//        sysUser.setUpdateUser(sysUser.getId());
//        sysUserMapper.updateById(sysUser);

        //此处还可以进行一些处理，比如登录成功之后可能需要返回给前台当前用户有哪些菜单权限，
        //进而前台动态的控制菜单的显示等，具体根据自己的业务需求进行扩展

//        //返回json数据
//        JsonResult result = ResultTool.success();
//        //处理编码方式，防止中文乱码的情况
//        httpServletResponse.setContentType("text/json;charset=utf-8");
//        //塞到HttpServletResponse中返回给前台
//        httpServletResponse.getWriter().write(JSON.toJSONString(result));

        Result result = Result.ok().message("登录成功");
        this.WriteJSON(response,request,result);//把数据返回到前端
    }
}
